<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2017/2/13
 * Time: 17:24
 */
namespace haibao\jiepai\business;

use haibao\classlibrary\Config;

class Files extends \haibao\jiepai\business\Base{

	/**
	 * 根据id获取图片
	 */
	public static function getImagesById($imageIds){
		if(!$imageIds){
			throw new BusinessException('图片ID传递错误');
		}
		$data = new \haibao\jiepai\data\Files();
		return $data->getImageById((array)$imageIds);
	}
	
    /**
     * @param $file     上传图片file
     * @param $savePath 存储文件相对路径
     */
    public static function saveFiles($file, $savePath, $onlyImage = false){
        $storagePath = \haibao\classlibrary\jiepai\Config::getConfig(\haibao\classlibrary\jiepai\Config::JIEPAI_IMAGE_STORAGE_BUCKET_1).$savePath;
        self::storageImage($storagePath, file_get_contents($file['tmp_name']));

        if(!$onlyImage) {
            list($width, $height, $size) = getimagesize($file['tmp_name']);
            $fileInfo = array(
                'width' => $width,
                'height' => $height,
                'size' => $size
            );

            $userId = self::$userMap[self::getCurrentUser()->Id]['id'];
            $data = new \haibao\jiepai\data\files();
            $data->saveImageInfo($fileInfo, $storagePath, $userId);
        }
    }

    public static function insertFile($fileName){
        $data = new \haibao\jiepai\data\files();
        return $data->insertFile($fileName);
    }

    /**
     * 存储图片
     */
    private static function storageImage($path, $stream){
        $savePath = self::getSaveImagePath($path);
        self::createDir($savePath);
        file_put_contents($savePath, $stream, LOCK_EX);
    }

    /**
     * 获取图片存储完整绝对路径
     */
    private static function getSaveImagePath($path){
        $path = '/'. ltrim($path,'/');
        return Config::getConfig(Config::STORAGE_PATH_UPLOADS) . ltrim($path, '/uploads/');
    }

    /**
     * 创建文件保存路径
     */
    public static function createDir($path){
        $path = pathinfo( $path );
        return is_writable($path['dirname']) ?: mkdir($path['dirname'], 750, true);
    }

    public static function saveUpload($file, $upSource, $isTemp = true){
        $storagePath = \haibao\cms\business\ImageLibrary::getUploadStoragePath($file['name']);
        $data = new \haibao\cms\data\ImageLibrary();

        $userId = \haibao\cms\model\data\ImageLibrary::QUESTION_DEFAULT_USER_ID;
        $userName = \haibao\cms\model\data\ImageLibrary::QUESTION_DEFAULT_USER_NAME;

        $picId = $data->saveImage($file['tmp_name'], $storagePath, $userId, $upSource,$userName, $isTemp);
        self::storageImage($storagePath, file_get_contents($file['tmp_name']));

        return array(
            'id' => $picId,
            'imageUrl' => $storagePath
        );
    }

    public static function requestFileInfo($pictures){

        $pictureIds = array();

        $imageData = new \haibao\cms\data\ImageLibrary();
        foreach($pictures as $picture){
            $picUrl = Config::getConfig(Config::CLASSLIBRARY_CONFIG_STATIC_ONE_DOMAIN).'/jiepai-1/'.$picture.'@info';
            $picUrl = 'http://s1.haibao.cn/jiepai-1/'.$picture.'@info';

            $picInfo = file_get_contents($picUrl);
            if($picInfo){
                $picInfo = json_decode($picInfo,true);
                $pictureEnd = explode('@', $picture);
                $picInfo['FileName'] = $pictureEnd[0];
                    $pictureId = $imageData->insertCommentPictures($picInfo);
                array_push($pictureIds, $pictureId);
            }
        }
        return $pictureIds;
    }
    
}